<template>
    <div class="flex bottomBox">
        <div class="boxShadow updateCom">
            <p class="boldTitle">软件更新</p>
            <div class="flex modulContent">
                <el-radio-group v-model="updateType" @change="chooseUpdate">
                    <div class="updateItem"><el-radio :label="onLineAuto">在线自动更新</el-radio></div>
                    <div class="updateItem"><el-radio :label="offLineAuto">离线本地更新</el-radio></div>
                    <div class="updateItem"><el-radio :label="dbparam">U盘威尔2升级包</el-radio></div>
                    <div class="updateItem"><el-radio :label="openssh">U盘安装文件传输包</el-radio></div>
                </el-radio-group>
            </div>

            <div class="flex formBtns">
                <el-button size="small" type="primary" @click="subUpdate">立即更新</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { submitupdate } from '../js/methods'
export default {
    name: 'update',
    components:{

    },
    data(){
        return{
            onLineAuto:'online',
            offLineAuto:'offline',
            dbparam:'dbparam',
            openssh:'openssh',
            updateType: '',
            updateString:'',
            updateTypeString:{
                onlineAuto: "update",
                offlineAuto: `if [ -f ~/release-$ROS_DISTRO.zip ]; 
                                then unzip ~/release-$ROS_DISTRO.zip; 
                                release-$ROS_DISTRO/install/share/bringup/shell/update-offline.sh; 
                                rm -r release-$ROS_DISTRO; 
                                rm ~/release-$ROS_DISTRO.zip; 
                                rostopic pub -1 /shell_feedback std_msgs/String "update:success"; 
                            else  
                                rostopic pub -1 /shell_feedback std_msgs/String "update:failure"; 
                            fi;`,
                dbparam: 'if [ -f /media/`whoami`/*/dbparam-$ROS_DISTRO.zip ]; then unzip /media/`whoami`/*/dbparam-$ROS_DISTRO.zip; cd dbparam-$ROS_DISTRO; ./install.sh; cd ..; rm -r dbparam-$ROS_DISTRO; rm ~/dbparam-$ROS_DISTRO.zip; rostopic pub -1 /shell_feedback std_msgs/String "update:success"; else  rostopic pub -1 /shell_feedback std_msgs/String "update:failure"; fi;',
                openssh: 'if [ -f /media/`whoami`/*/openssh-$ROS_DISTRO.zip ]; then unzip /media/`whoami`/*/openssh-$ROS_DISTRO.zip; cd openssh-$ROS_DISTRO; ./install.sh; cd ..; rm -r openssh-$ROS_DISTRO; rostopic pub -1 /shell_feedback std_msgs/String "update:success"; else  rostopic pub -1 /shell_feedback std_msgs/String "update:failure"; fi;',
                apache: 'if [ -f ~/apache-$ROS_DISTRO.zip ]; then unzip ~/apache-$ROS_DISTRO.zip; cd apache-$ROS_DISTRO; ./install.sh; cd ..; rm -r apache-$ROS_DISTRO; rm ~/apache-$ROS_DISTRO.zip; rostopic pub -1 /shell_feedback std_msgs/String "update:success"; else  rostopic pub -1 /shell_feedback std_msgs/String "update:failure"; fi;',
            }
        }
    },
    methods:{
      chooseUpdate:function(e){
          let that = this;
          switch(e){
              case 'online':
              that.$message({
                    showClose: true,
                    message: '请确认导航板可以连接网络！',
                    type: 'warning'
                });
                that.updateString = that.updateTypeString.onlineAuto;
              break;

              case 'offline':
              that.$message({
                    showClose: true,
                    message: '请先把dbparam-***.zip文件放置在pkg文件夹中，然后运行update脚本！',
                    type: 'warning'
                });
                that.updateString = that.updateTypeString.offlineAuto;
              break;

              case 'dbparam':
              that.$message({
                    showClose: true,
                    message: '请先把dbparam-***.zip文件放置在U盘根目录下插入导航板！',
                    type: 'warning'
                });
                that.updateString = that.updateTypeString.dbparam;
              break;

              case 'openssh':
              that.$message({
                    showClose: true,
                    message: '请先把openssh-***.zip文件放置在U盘根目录下插入导航板！',
                    type: 'warning'
                });
                that.updateString = that.updateTypeString.openssh;
              break;
          }
      },

      /**
       * 提交更新
       * **/
      subUpdate:function(){
          if(this.updateType === ''){
            this.$message({
                showClose: true,
                message: '请选择更新类型',
                type: 'warning'
            });
            return
          }
          submitupdate(this.updateType, this.updateString)
      }
    },

    //属性监听
    watch:{
       
    }

}
</script>

<style scoped lang="less">
 .updateCom{
     background-color: #ffffff;
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     padding: 10px;
     text-align: left;
     font-size: 14px;
     width: 100%;
 }
 .boldTitle{
     font-weight: bold;
 }
 .modulContent{
     color: #666666;
     margin: 16px 0 30px 0 ;
     .updateItem{
         margin-top: 14px
     }
 }
 .formBtns{
     padding: 0 5px;
     margin-top: 15px;
     button{
         width: 100%;
     }
 }
</style>